#include "bPer_player_app.h"
#include "bperutil/BPerLoadLib.h"
#include <stdio.h>
void* (*bPer_create_player)();
void (*bPer_destroy_player)(void* user);
int32_t (*bPer_player_playRtc)(void* user,char* url);
int32_t (*bPer_player_close)(void* user);
uint8_t* (*bPer_player_get_buffer)(void* user,int32_t width,int32_t height);
BPerLoadLib *g_bPer_lib=nullptr;

void BPerPlayerApp::loadLib() {
    bPer_create_player = (void *(*)())g_bPer_lib->loadFunction(
        "bPer_create_player");
    bPer_destroy_player = (void (*)(void *user))g_bPer_lib->loadFunction(
        "bPer_destroy_player");
    bPer_player_playRtc = (int32_t(*)(void *user, char *url))g_bPer_lib->loadFunction(
        "bPer_player_playRtc");
    bPer_player_close = (int32_t(*)(void *user))g_bPer_lib->loadFunction(
        "bPer_player_close");
    bPer_player_get_buffer = (uint8_t * (*)(void *user, int32_t width, int32_t height)) g_bPer_lib->loadFunction(
        "bPer_player_get_buffer");
}

    BPerPlayerApp::BPerPlayerApp(){
        m_player=nullptr;
        if(g_bPer_lib==nullptr)
            g_bPer_lib=new BPerLoadLib();
        g_bPer_lib->loadObject("libmetaplayer7");
 
        loadLib();
        if(bPer_create_player)
            m_player=bPer_create_player();
    }
   BPerPlayerApp::~BPerPlayerApp(){
    
    if(m_player&&bPer_destroy_player) {
        bPer_destroy_player(m_player);
        m_player=nullptr;
    }
    if(g_bPer_lib) delete(g_bPer_lib);
   }

   int32_t BPerPlayerApp::playRtc(char *url) {
    return bPer_player_playRtc(m_player,url);
    
    }

    int32_t BPerPlayerApp::close()
    {
        return bPer_player_close(m_player);
        
    }

   uint8_t *BPerPlayerApp::getPlayBuffer(int32_t width, int32_t height) {
    return bPer_player_get_buffer(m_player,width,height);
   }
